<?php

namespace services\log;

use common\components\Service;
use common\models\log\LogLogin;
use common\models\member\Member;
use Yii;

class LogLoginService extends Service
{
    /**
     * 登陆日志
     * @param Member $member
     * @param string $group 组别
     * @param int $loginType 类型
     * @return void
     * @throws \Exception
     */
    public static function loginLog(Member $member, string $group, int $loginType)
    {
        $data = Yii::$app->services->log->getLogData();

        $login = new LogLogin();
        $login->group = $group;
        $login->mobile = $data['post_data']['username'] ?? '';
        $login->member_id = $member->id;
        $login->member_name = $member->realname;
        $login->login_type = $loginType;
        $login->method = $data['method'];
        $login->module = $data['module'];
        $login->controller = $data['controller'];
//        $login->header_data = str_replace('"', '', CommonHelper::detectVersion());
        $login->header_data = Yii::$app->request->getHeaders();
//        $login->device = CommonHelper::getOS();
        $login->action = $data['action'];
        $login->url = $data['url'];
        $login->ip = (string)$data['ip'];
        $login->error_code = $data['error_code'] ?: 0;
        $login->error_msg = $data['error_msg'] ?: '';
        $login->error_data = json_encode($data['error_data']);
        $login->saveAndCheckResult();
    }
}